// pages/login/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
     data: {
          mobile: '',
          send_time: 0, //再次获取验证码时间 倒计时
          send_count: 1, //第一次发送验证码
          userInfo: {},
          code_id: '', //短信验证码存在数据库的id
          kefu: false
     },

	/**
	 * 生命周期函数--监听页面加载
	 */
     onLoad: function (options) {
          wx.setNavigationBarTitle({
               title: '绑定手机号'
          })
          let that = this;
          app.login(function (userInfo) {
               that.setData({
                    userInfo: userInfo.data
               });
          });
     },

	/**
	 * 获取手机验证码
	 */
     getCode: function (e) {

          let that = this;
          let mobile = that.data.mobile;
          console.log(mobile);
          if (!app.isPoneAvailable(mobile)) {
               wx.showToast({
                    title: '不是有效的手机号',
                    icon: 'none'
               })
               return false;
          }
          //检测手机号是否被占用，如果占用不能继续注册
          app.connect('login/checkMobile', {
               openid: that.data.userInfo.openId,
               uid: that.data.userInfo.uid,
               mobile: mobile
          }, function (res) {
               //被占用 提示
               if (res.code != 1) {
                    wx.showToast({
                         title: '手机号已被占用',
                         icon: 'none'
                    })
               } else {
                    //获取验证码按钮更改样式
                    that.setData({
                         send_time: 60
                    })
                    var referId = setInterval(function () {
                         if (that.data.send_time == 0) {
                              clearInterval(referId);

                              return false;
                         }
                         if (that.data.send_time == 1) {
                              that.setData({
                                   send_count: that.data.send_count + 1
                              })
                         }
                         that.setData({
                              send_time: that.data.send_time - 1
                         })
                    }, 1000);
                    //发送验证码  
                    app.connect('login/sendCode', {
                         openid: that.data.userInfo.openId,
                         uid: that.data.userInfo.uid,
                         mobile: mobile
                    }, function (res) {
                         console.log(res);
                         if (res.code_id != undefined) {
                              that.setData({
                                   code_id: res.code_id
                              });
                              wx.showToast({
                                   title: '验证码发送成功'
                              })
                         } else {
                              wx.showToast({
                                   title: res.msg,
                                   icon: 'none'
                              })
                              console.log(res);
                         }
                    });
               }
          })

     },
     //手机号输入监听
     input: function (e) {
          this.setData({
               mobile: e.detail.value
          });
     },
     submit: function (e) {
          console.log(e);
          var code = e.detail.value.code;
          let that = this;
          //校验验证码是否正确
          app.connect('login/checkCode', {
               uid: that.data.userInfo.uid,
               openid: that.data.userInfo.openId,
               code_id: that.data.code_id,
               mobile: that.data.mobile,
               code: code
          }, function (res) {
               console.log(res);

               if (res.code == 1) {
                    //验证成功 进行下一步操作
                    app.login();
                    wx.showToast({
                         title: '绑定手机号成功'
                    })
                    wx.reLaunch({
                         url: '../my/index',
                    })
               } else {
                    //
                    wx.showToast({
                         title: res.msg,
                         icon: 'none'
                    })
               }
          });
     },
     kefu: function () {
          this.setData({
               kefu: true
          });
     },
     copy: function () {
          wx.setClipboardData({
               data: 'maggiekf01',
               success: function (res) {

               }
          })
     }
})